package main

import (
	pb "ihos/ihos"
	"log"

	tm "github.com/golang/protobuf/ptypes/timestamp"
	"github.com/lib/pq"
)

type shipin_yonghu struct {
	id     int32         //PK
	spxx   int32         //视频信息 => shipin PK
	yhid   int32         //用户ID => huanzhe/kefu/yisheng PK
	yhlx   int32         //用户类型
	jrsj   int64         //加入时间
	lksj   int64         //离开时间
	xggh   pq.Int64Array //相关挂号
	online bool          //是否还在线
}

func (w *shipin_yonghu) GetFromDB(id int32) error {
	sql := "SELECT id,spxx,yhid,yhlx,jrsj,lksj,xggh,online FROM shipin_yonghu WHERE id=$1"
	return DBConn.db.QueryRow(sql, id).Scan(&w.id, &w.spxx, &w.yhid, &w.yhlx, &w.jrsj, &w.lksj, &w.xggh, &w.online)
}

func (w *shipin_yonghu) GetFromDBByYh() error {
	sql := "SELECT id,spxx,yhid,yhlx,jrsj,lksj,xggh,online FROM shipin_yonghu WHERE yhid=$1 AND yhlx=$2 ORDER BY ID DESC LIMIT 1"
	return DBConn.db.QueryRow(sql, w.yhid, w.yhlx).Scan(&w.id, &w.spxx, &w.yhid, &w.yhlx, &w.jrsj, &w.lksj, &w.xggh, &w.online)
}

// id: PK of record to be save
//
//	0  - insert
//	>0 - update
func (w *shipin_yonghu) SaveToDB(id int32) error {
	log.Println("shipin_yonghu SaveToDB:", w)
	sql := ""
	var err error
	if id > 0 { //update
		sql = "UPDATE shipin_yonghu set spxx=$1,yhid=$2,yhlx=$3,jrsj=$4,lksj=$5,xggh=$6,online=$7 where id=$8 RETURNING id"
		err = DBConn.db.QueryRow(sql, w.spxx, w.yhid, w.yhlx, w.jrsj, w.lksj, w.xggh, w.online, w.id).Scan(&w.id)
	} else { //insert
		sql = "INSERT INTO shipin_yonghu(spxx,yhid,yhlx,jrsj,lksj,xggh,online) VALUES($1,$2,$3,$4,$5,$6,$7) RETURNING id"
		err = DBConn.db.QueryRow(sql, w.spxx, w.yhid, w.yhlx, w.jrsj, w.lksj, w.xggh, w.online).Scan(&w.id)
	}
	log.Println("shipin_yonghu SaveToDB", w, err)
	// log.Println(sql)
	return err
}

func (w *shipin_yonghu) UpdateOnline() error {
	sql := ""
	var err error
	if w.id > 0 { //update
		sql = "UPDATE shipin_yonghu set online=$1 where id=$2 RETURNING id"
		err = DBConn.db.QueryRow(sql, w.online, w.id).Scan(&w.id)
	}
	log.Println("shipin_yonghu UpdateOnline", w, err)
	// log.Println(sql)
	return err
}

func (w *shipin_yonghu) fromPB(p *pb.ShiPinYongHuL5) *shipin_yonghu {
	// log.Println("shipin_yonghu fromPB", p)
	w.id = p.GetID()
	w.spxx = p.GetSPXX().GetID()
	w.yhid = p.GetYHID()
	w.yhlx = int32(p.GetLX())
	w.jrsj = p.GetJRSJ().GetSeconds()
	w.lksj = p.GetLKSJ().GetSeconds()
	// p.GetXGGHS()
	for _, v := range p.GetXGGHS() {
		w.xggh = append(w.xggh, int64(v.GetID()))
	}
	w.online = p.GetONLINE()
	// log.Println("w.xggh:", w.xggh)
	return w
}

func (w *shipin_yonghu) toPB() *pb.ShiPinYongHuL5 {
	p := &pb.ShiPinYongHuL5{
		ID:     w.id,
		SPXX:   &pb.ShiPinInfoL1{ID: w.spxx},
		YHID:   w.yhid,
		YHXX:   &pb.BasicInfoL1{},
		LX:     pb.YHLX(w.yhlx),
		JRSJ:   &tm.Timestamp{Seconds: w.jrsj},
		LKSJ:   &tm.Timestamp{Seconds: w.lksj},
		XGGHS:  make([]*pb.GuaHao, 0),
		ONLINE: w.online,
	}
	if p.GetSPXX().GetID() > 0 {
		s := &shipin{
			id: p.GetSPXX().GetID(),
		}
		if err := s.GetFromDB(s.id); err == nil {
			p.SPXX = s.toPB()
		}

	}
	// log.Println("shipin_yonghu:", w.yhid)
	bid := int32(0)
	switch pb.YHLX(w.yhlx) {
	case pb.YHLX_HUANZHE:
		h := &huanzhe{id: p.GetYHID()}
		if err := h.GetFromDB(h.id); err != nil {
			bid = 0
		} else {
			bid = h.grxx
		}
	case pb.YHLX_KEFU:
		h := &kefu{id: p.GetYHID()}
		if err := h.GetFromDB(h.id); err != nil {
			bid = 0
		} else {
			bid = h.grxx
		}
	case pb.YHLX_YISHENG:
		h := &yisheng{id: p.GetYHID()}
		if err := h.GetFromDB(h.id); err != nil {
			bid = 0
		} else {
			bid = h.grxx
		}
	}
	// log.Println("shipin_yonghu:", bid)
	// if p.GetYHXX().GetID() > 0 {
	if bid > 0 {
		u := &basicinfo{id: bid}
		if err := u.GetFromDB(u.id); err == nil {
			p.YHXX = u.toPB()
		}
	}
	for _, v := range w.xggh {
		g := new(guahao)
		if err := g.GetFromDB(int32(v)); err != nil {
		} else {
			p.XGGHS = append(p.XGGHS, g.toPB())
		}
	}
	// log.Println("ShiPinYongHuL5:", p)
	return p
}
